<?php 
require_once 'ckLogin.php';
require_once 'init.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();
if(!empty($_GET['act'])){
	$act = $_GET['act'];
	if($act == 'quiz_del'){
		$db->sql = "DELETE FROM fun_quiz WHERE id = ".$_GET['id'];
		$db->doExecute();
		$db->sql = "DELETE FROM fun_quiz_result WHERE quiz_id = ".$_GET['id'];
		$db->doExecute();
	}elseif ($act == "quiz_result_del"){
		$db->sql = "DELETE FROM fun_quiz_result WHERE id = ".$_GET['id'];
		$db->doExecute();
	}
}

//
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	$option = $_POST['option_words'];
	$result = $_POST['result_words'];
	$quiz_id = $_POST['quiz_id'];
	$db->sql ="INSERT INTO fun_quiz_result(quiz_id,`option`,result,create_time,create_user) VALUE(".$quiz_id.",'".$option."','".$result."',UNIX_TIMESTAMP(),'".$_SESSION['userName']."');";
	$db->doExecute();
}

//获取
$db->sql = "SELECT * FROM fun_quiz";
$questions = $db->doResultList2();

foreach ($questions as $key => $item){
	$db->sql = "SELECT * FROM fun_quiz_result WHERE quiz_id = ".$item['id'];
	$rs = $db->doResultList2();
	$questions[$key]['rs']=$rs;
}
//var_dump($questions);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo APPLICATION;?></title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css" media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css" media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css" media="screen" />

<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	$(".add_option_result").click(function(){
		var id = $(this).parents("tr").children("td").eq(0).text();
		//alert(id);
		var new_option_result = '<div class="add_option_result_div"><div><form action="funQuiz.php" method="post">'+
			'<input id="option_words" name="option_words" class="text-input " type="text" value=""/><br />'+
			'<input id="result_words" name="result_words" class="text-input " type="text" value=""/><br />'+
			'<input id="quiz_id" name="quiz_id" class="text-input " type="hidden" value="'+id+'"/><br />'+
			'<a href="javascript:void(0);" class="add_option_result_submit">提交</a>'+
			'</form></div>'+
			'<div style="clear:both;height:1px;background:#9A9A9A;"></div>'+
			'</div>';
		$(this).siblings(".option_result").append(new_option_result);

		$(".add_option_result_submit").click(function(){
			var option_words = $(this).siblings("#option_words").val();
			var result_words = $(this).siblings("#result_words").val();
			var quiz_id = $(this).siblings("#quiz_id").val();
			//alert(quiz_id);
			$(this).parents('form').submit();
			$(".add_option_result_div").remove();
		});
	});
	
	$(".success").ajaxSuccess(function(evt, request, settings){
		$(this).show();
	});
	$(".error").ajaxError(function(event,request, settings){
		$(this).show();
	});

	$(".deleteUser").click(function(){
		if(!confirm("是否删除这个趣味问题？")){
			return false;
		}
	});
});
</script>
<!-- jQuery Configuration -->
<script type="text/javascript"
	src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>

</head>

<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->
		
		<div id="main-content">
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>

			<div class="clear"></div>
			<!-- End .clear -->

			<div class="content-box">
				<!-- Start Content Box -->

				<div class="content-box-header">

					<h3>趣味问答</h3>

					<div class="clear"></div>

				</div>
				<!-- End .content-box-header -->

				<div class="content-box-content">

				<!-- Start Notifications -->
				
				<div class="notification success png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
						修改成功
					</div>
				</div>
				
				<div class="notification error png_bg" style="display: none;">
					<a href="#" class="close"><img
						src="resources/images/icons/cross_grey_small.png"
						title="Close this notification" alt="close" /></a>
					<div>
					修改失败
					</div>
				</div>
				
				<!-- End Notifications -->
				
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>
							<thead>
								<tr>
									<th style="width: 10%">编号</th>
									<th style="width: 30%">问题</th>
									<th style="width: 10%">操作</th>
									<th style="width: 50%">答案/结果</th>
								</tr>

							</thead>

							<tbody>
							<?php if(count($questions)>0):?>
							<?php foreach ($questions as $key=>$item):?>
								<tr>
									<td><?php echo $item['id'];?></td>
									<td><?php echo $item['question'];?></td>
									<td>
										<a href="funQuizAdd.php?act=edit&id=<?php echo $item['id'];?>">修改</a>
										<a class="deleteUser" href="funQuiz.php?act=quiz_del&id=<?php echo $item['id'];?>&">删除</a>
									</td>
									
									<td>
										<div style="width: 90%">
										<div class="option_result">
											<?php foreach ($item['rs'] as $key2 => $item2): //var_dump($item2);?>
											<div>
												<div>
													<p style="font-weight: bold;"><?php echo $item2['option'];?></p>
													<span><?php echo $item2['result'];?></span>
												</div>
												<div style="float: right;position: relative;top: -40px;">
													<a class="deleteUser" href="funQuiz.php?act=quiz_result_del&id=<?php echo $item2['id'];?>&">删除</a>
												</div>
												<div style="clear:both;height:1px;background:#9A9A9A;"></div>
											</div>
											<?php endforeach;?>
										</div>
										<a href="javascript:void(0);" class="add_option_result">添加（答案/结果）</a>
										</div>
									</td>
									
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="5" style="text-align: center;">一个趣味问题也没添加！</td></tr>
							<?php endif;?>
							</tbody>

						</table>

					</div>
					<!-- End #tab2 -->

				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
